package com.itheima.d4_synchronized_method;

public class Account {
    private String accountId; //卡号
    private double money; //账户余额

    public Account(String accountId, double money) {
        this.accountId = accountId;
        this.money = money;
    }

    //省略getter、setter方法

    //取钱功能
    public void drawMoney(double money) {
        Thread thread = Thread.currentThread();
        String name = thread.getName();

        process(money,name);
    }

    //方法改造为同步方法
    public synchronized void process(double money, String name) {

            if (this.money >= money) { //账户余额充足
                System.out.println(name + "成功取钱" + money + "元");
                this.money -= money;

                System.out.println("账户余额为：" + this.money + "元");
            }else {
                System.out.println(name +"余额不足取款失败～");
            }
    }


    @Override
    public String toString() {
        return "Account{" +
                "accountId='" + accountId + '\'' +
                ", money=" + money +
                '}';
    }
}
